package com.example.swd1.gudong.main.user.systemoption;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.swd1.gudong.R;

/**
 * Created by user on 9/7/16.
 */
public class OptionTextPreference extends Preference {
    private String text;
    private String value;
    public OptionTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取preferences中自定义参数，填充到TextPreference对象
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextPreference);
        text = a.getString(R.styleable.TextPreference_text);
        value = a.getString(R.styleable.TextPreference_value);
        a.recycle();
    }

    public OptionTextPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        TextView keyView = (TextView) view.findViewById(R.id.tv_preference_key);
        TextView valueView = (TextView) view.findViewById(R.id.tv_preference_value);
        keyView.setText(text);
        valueView.setText(value);
    }

//    自己写一个加载时获取preference数据

    @SuppressLint("MissingSuperCall")
    @Override
    protected View onCreateView(ViewGroup parent) {
        //super.onCreateView(parent);
        return LayoutInflater.from(getContext()).inflate(R.layout.preference_option_text, parent, false);
    }

    @Override
    protected void onClick() {
        super.onClick();
        Toast.makeText(getContext(), "onClick", Toast.LENGTH_SHORT).show();
    }
}
